<template>
  <div class="chart-container">
    <VChart v-if="props.data.length > 0" :option="chartOptions" autoresize />
  </div>
</template>

<script setup>
import { computed } from 'vue'
import VChart from 'vue-echarts'
import { use } from 'echarts/core'

// 引入 ECharts 模块
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
} from 'echarts/components'
import { LineChart } from 'echarts/charts'
import { CanvasRenderer } from 'echarts/renderers'

use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  LineChart,
  CanvasRenderer
])

// 接收 props
const props = defineProps({
  data: {
    type: Array,
    default: () => [],
  }
})

// 生成配置项
const chartOptions = computed(() => {
  const xAxisData = props.data.map(item => item.x)
  const yAxisData = props.data.map(item => item.y)

  return {
    title: { text: '', left: 'center' },
    tooltip: { trigger: 'axis' },
    xAxis: {
      type: 'category',
      data: xAxisData
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '值',
        data: yAxisData,
        type: 'line',
        smooth: true,
        lineStyle: {
          color: '#66BB6A'
        },
        itemStyle: {
          color: '#66BB6A'
        }
      }
    ]
  }
})
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 400px;
}
</style>
